import Mock from "mockjs"
let { data } = Mock.mock({
    // 20条数据
    "data|120": [{
        "id|+1": 1,
        // 标题
        "title": " @ctitle(6)",
        // 作者
        "writer": "@cname",
        //分类
        "classify|1": ["女装", "才艺", "颜值", "游戏"],
        //时间 
        "times": "@time",
        //推荐度
        "sell|0-100": 0,
        //已发布
        "set|1": ["已发布", "未发布"],


    }]
})
// 输出结果随机生成的数据（node index.js）
console.log(data);


// 列表接口，分页，搜索
Mock.mock("/api/pos/list", "post", (options: any) => {
    const { pageSize, currentPage, search } = JSON.parse(options.body);

    if (pageSize && currentPage) {
        let result;
        let count = 0;
        if (search) {
            result = data
                .filter((item: any) => item.doctor === search)
                .slice((currentPage - 1) * pageSize, currentPage * pageSize);
            count = data.filter((item: any) => item.doctor === search).length;
        } else {
            result = data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
            count = data.length;
        }
        return {
            status: 0,
            result: result,
            count: count,
        };
    } else {
        return {
            status: 1,
            result: "参数错误",
        };
    }
});

// 删除接口
Mock.mock("/api/pos/del", "post", (options: any) => {
    const { id } = JSON.parse(options.body);
    if (id) {
        data = data.filter((item: any) => item.id !== id);
        return {
            status: 0,
            msg: "删除成功",
        };
    } else {
        return {
            status: 1,
            msg: "参数错误",
        };
    }
});

export default Mock;